package cn.yuxiaoyuan.article.service.impl;

import afu.org.checkerframework.checker.igj.qual.I;
import cn.yuxiaoyuan.article.bo.ArticleTypeCountBo;
import cn.yuxiaoyuan.article.domain.Article;
import cn.yuxiaoyuan.article.mapper.ArticleMapper;
import cn.yuxiaoyuan.article.mongo.BlogArticle;
import cn.yuxiaoyuan.article.query.ArticleQuery;
import cn.yuxiaoyuan.article.service.ArticleService;
import cn.yuxiaoyuan.base.service.impl.BaseServiceImpl;
import cn.yuxiaoyuan.constant.RedisConstant;
import io.swagger.models.auth.In;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class ArticleServiceImpl extends BaseServiceImpl<Article> implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void add(Article article) {
        Date date = new Date();
        article.setCreateTime(date);
        article.setUpdateTime(date);
        article.setArticleCommentNum(0);
        article.setArticleReadCount(0);
        article.setArticleStarNum(0);
//        todo 登录做了之后才可以做
//        article.setPublishId();
        super.add(article);
        BlogArticle blogArticle = BlogArticle.of(article);
        mongoTemplate.insert(blogArticle);

    }

    @Override
    public void update(Article article) {
        super.update(article);
        Query query = new Query(Criteria.where("id").is(article.getId()));
        BlogArticle one = mongoTemplate.findOne(query, BlogArticle.class);
        BeanUtils.copyProperties(article,one);
        mongoTemplate.save(one);
    }

    @Override
    public void deleteById(Long id) {
        super.deleteById(id);
        mongoTemplate.remove(new Query(Criteria.where("id").is(id)),BlogArticle.class);
    }

    @Override
    public List<Article> queryPage(ArticleQuery query) {
        return articleMapper.queryPage(query);
    }

    @Override
    public List<Article> selectPage(ArticleQuery articleQuery) {
        return articleMapper.selectPage(articleQuery);
    }

    @Override
    public Integer star(HttpServletRequest request, Long id) {
        //获取当前人的ip地址
        String localAddr = request.getRemoteAddr();
        System.out.println(localAddr);
        if(!redisTemplate.hasKey(RedisConstant.ARTICLE_STAR+localAddr)){
            redisTemplate.opsForValue().set(RedisConstant.ARTICLE_STAR+localAddr,1,24, TimeUnit.HOURS);
            Article article = articleMapper.findById(id);
            article.setArticleStarNum(article.getArticleStarNum()+1);
            articleMapper.update(article);
            return article.getArticleStarNum();
        }
        return null;
    }

    @Override
    public List<ArticleTypeCountBo> articleTypeCount() {
        return articleMapper.articleTypeCount();
    }

    @Override
    public Map<Integer, List<Article>> archives() {
        //所有数据
        List<Article> all = articleMapper.findAll();
        Map<Integer, List<Article>> collect = all.stream()
                .filter(article -> article.getUpdateTime()!=null)//过滤哪些为null的数据
                .filter(article -> article.getArticleState()!=1)//过滤哪些为null的数据
                .collect(Collectors.groupingBy(article -> {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(article.getUpdateTime());
                    return calendar.get(Calendar.YEAR);
                }));
//        Map<Integer,List<Article>> map = new HashMap<>();
//        for (Article article : all) {//2001 2002 2003
//            if(article.getArticleState() != 1){
//                continue;
//            }
//            if(article.getUpdateTime() == null){
//                continue;
//            }
//            Calendar calendar = Calendar.getInstance();
//            calendar.setTime(article.getUpdateTime());
//            int year = calendar.get(Calendar.YEAR);
//
//            if(map.get(year) == null){  //判断map里面有没有这个年份
//                ArrayList<Article> objects = new ArrayList<>();
//                objects.add(article);
//                map.put(year,objects);
//            }else {
//                List<Article> articles = map.get(year);
//                articles.add(article);
//            }
//        }

        return collect;
    }
}
